#include <gtk/gtk.h>

GtkWidget * window;
GtkWidget * keyword_entry_0;
GtkWidget * file_button;
GtkWidget * output_window;
GtkWidget * output;
GtkWidget * service_entry;
GtkWidget * username_entry;
GtkWidget * length_label;
GtkWidget * length_entry;
GtkWidget * special_entry;
GtkWidget * keyword_entry_1;
GtkWidget * single_button;

GtkTextBuffer * output_buffer;

GtkWidget * grid;

GtkWidget * create(GtkWidget * widget, int x, int y, int w, int h)
{
	gtk_grid_attach(GTK_GRID(grid), widget, x, y, w, h);
	return widget;
}

GtkWidget * gtk_custom_entry_new(const gchar * placeholder, gboolean hidden)
{
	GtkWidget * entry = gtk_entry_new();
	gtk_entry_set_placeholder_text(GTK_ENTRY(entry), placeholder);
	gtk_entry_set_visibility(GTK_ENTRY(entry), !hidden);
	return entry;
}

void builder()
{
	// Create main window
	window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_window_set_title           (GTK_WINDOW   (window), "Crypt");
	gtk_window_set_resizable       (GTK_WINDOW   (window), FALSE);
	gtk_container_set_border_width (GTK_CONTAINER(window), 3);
	g_signal_connect               (window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
	// Create grid
	grid = gtk_grid_new();
	gtk_grid_set_row_spacing(GTK_GRID(grid),    2);
	gtk_grid_set_column_spacing(GTK_GRID(grid), 2);
	gtk_container_add(GTK_CONTAINER(window), grid);
	// Create widgets
	keyword_entry_0 = create(gtk_custom_entry_new("Keyword", TRUE),           0,0,1,1);
	file_button     = create(gtk_button_new_with_label("Generate from file"), 3,0,2,1);
	output_buffer   = gtk_text_buffer_new(NULL);
	output          = gtk_text_view_new_with_buffer(output_buffer);	
	output_window   = create(gtk_scrolled_window_new (NULL, NULL),            0,1,5,1);
	service_entry   = create(gtk_custom_entry_new("Service", FALSE),          0,2,1,1);
	username_entry  = create(gtk_custom_entry_new("Username", FALSE),         1,2,1,1);
	length_label    = create(gtk_label_new("Length"),                         2,2,1,1);
	length_entry    = create(gtk_spin_button_new_with_range(8,16,1),          3,2,1,1);
	special_entry   = create(gtk_check_button_new_with_label("Special"),      4,2,1,1);
	keyword_entry_1 = create(gtk_custom_entry_new("Keyword", TRUE),           0,3,1,1);
	single_button   = create(gtk_button_new_with_label("Generate single"),    3,3,2,1);
	// Configure output
	gtk_widget_set_size_request(output_window, 600, 600);
	gtk_container_add(GTK_CONTAINER(output_window), output);
	gtk_text_view_set_editable(GTK_TEXT_VIEW(output), FALSE);
	// Configure buttons
	gtk_widget_set_halign(file_button, GTK_ALIGN_END);
	gtk_widget_set_halign(single_button, GTK_ALIGN_END);

	gtk_widget_show_all(window);
}
